package defaultPackage;

public class ObjectManager 
{
	int oxygenTimer = 50, objectMoveTimer = 10;
	Object[] objects;
	Grid grid;
	MainClass main;
	
	int difX,difY;
    static int numObjects;
    boolean firstMove = true;
	
	public ObjectManager(Grid grid, MainClass main)
	{
		this.main = main;
		this.grid = grid;
		objects = new Object[22501];
	}
	
	public void objectInit()
	{
		 objects[0] = new Object (310,330,ObjectType.ResourceGatherer,grid);
	     objects[1] = new Object (310,310,ObjectType.LifeSupport,grid);
	     objects[2] = new Object (330,310,ObjectType.Door,grid);
	     objects[3] = new Object (370,360,ObjectType.Airlock,grid);
	     main.grid.tiles[37][36].changeType(tileType.Interior);
	}
	
	public void moveInShip(int i,int cy,int cx)
	{
		if (!objects[i].operational && objects[i].getTileOxygen()> grid.tiles[(objects[i].getX()/10) + cx][(objects[i].getY()/10)+cy].getOxygen() && !grid.tiles[(objects[i].getX()/10) + cx][(objects[i].getY()/10)+cy].type.getSolid() && objects[i].type.isMoveable() && !grid.tiles[(objects[i].getX()/10) +cx][(objects[i].getY()/10) + cy].isOccupied() && objects[i].getTileOxygen() > 0)
		{
			objects[i].setLastTileX((objects[i].getX()/10)+objects[i].getTrajectory());
			objects[i].setLastTileY((objects[i].getY()/10)+objects[i].getTrajectory());
			grid.tiles[objects[i].getX() /10][objects[i].getY() / 10].setOccupied(false);
			objects[i].setX(((objects[i].getX()/10) + cx)*10);
			objects[i].setY(((objects[i].getY()/10) + cy)*10);
			grid.tiles[objects[i].getX() /10][objects[i].getY() / 10].setOccupied(true);
			objects[i].setOperational(false);
		}
		if (objects[i].operational && objects[i].getTileOxygen()-objects[i].type.getWeight()> grid.tiles[(objects[i].getX()/10) + cx][(objects[i].getY()/10)+cy].getOxygen() && !grid.tiles[(objects[i].getX()/10) + cx][(objects[i].getY()/10)+cy].type.getSolid() && objects[i].type.isMoveable() && !grid.tiles[(objects[i].getX()/10) +cx][(objects[i].getY()/10) + cy].isOccupied() && objects[i].getTileOxygen() > 0)
		{//accounts for the initial force required in order to dismount a object assuming it is attached to a wall some how.
			objects[i].setLastTileX(objects[i].getX()/10);
			objects[i].setLastTileY(objects[i].getY()/10);
			grid.tiles[objects[i].getX() /10][objects[i].getY() / 10].setOccupied(false);
			objects[i].setX(((objects[i].getX()/10) + cx)*10);
			objects[i].setY(((objects[i].getY()/10) + cy)*10);
			grid.tiles[objects[i].getX() /10][objects[i].getY() / 10].setOccupied(true);
			objects[i].setOperational(false);
		}
	}
	
	
	public void updateObjects()
    {
    	for (int i = 0; i < numObjects;i++)
    	{
    		if (objects[i].type.isGeneratingOxygen() && objects[i].isOperational())
    		{
    			oxygenTimer -= 1;
    			if (oxygenTimer == 0)
    			{
    				grid.tiles[(objects[i].getX() / 10)][(objects[i].getY() / 10)].addOxygen(2);
    				oxygenTimer = 1;
    			}
    		}
    		
    		if (objects[i].type.isGeneratingResources() && objects[i].isOperational())
    		{
    			Resources.resourceChance();
    		}
    		if (objects[i].getX() > Grid.dimensionx * 10 || objects[i].getY() > Grid.dimensiony * 10 || objects[i].getX() < 0 || objects[i].getY() < 0)
    			deleteObject(i);
    		
    		if (objectMoveTimer == 0)
    		{
    				
	    			for (int cy = -1;cy <= 1;cy++)
	    			{
	    				for (int cx = -1;cx<= 1;cx++)
	    				{
	    					if (objects[i].getTrajectory() == 1)
	    						moveInShip(i,cy,cx);
	    					else
	    						moveInShip(i,cx,cy);
	    				}
	    			}
    			if (objects[i].getTileOxygen() <= 10 && objects[i].type.isMoveable() && grid.tiles[objects[i].getX() /10][objects[i].getY() / 10].getType() == tileType.Space)
				{		
    				difX = ((objects[i].getX()/10 - objects[i].getLastTileX()));
    				difY = ((objects[i].getY()/10 - objects[i].getLastTileY()));
					objects[i].setLastTileX(objects[i].getX()/10);
					objects[i].setLastTileY(objects[i].getY()/10);
					grid.tiles[objects[i].getX() /10][objects[i].getY() / 10].setOccupied(false);
					if (!grid.tiles[objects[i].getX()/10 + (difX)][objects[i].getY()/10 + (difY)].isOccupied() && !grid.tiles[objects[i].getX()/10 + (difX)][objects[i].getY()/10 + (difY)].type.getSolid())
					{
						objects[i].setX(objects[i].getX() + (difX * 10));
						objects[i].setY(objects[i].getY() + (difY * 10));
					}
					else
					{
						if (grid.tiles[objects[i].getX()/10 + difX][objects[i].getY()/10].isOccupied() || grid.tiles[objects[i].getX()/10 + difX][objects[i].getY()/10].type.getSolid() && difY != 0)
						{
								difX = -1*difX;
						}
						else if (grid.tiles[objects[i].getX()/10][objects[i].getY()/10 + difY].isOccupied() || grid.tiles[objects[i].getX()/10][objects[i].getY()/10 + difY].type.getSolid() && difX != 0)
						{
							difY = -1*difY;	
						}
						else
						{
							difX = 0;
							difY = 0;
						}
						objects[i].setX(objects[i].getX() + (difX * 10));
						objects[i].setY(objects[i].getY() + (difY * 10));
					}
					grid.tiles[objects[i].getX() /10][objects[i].getY() / 10].setOccupied(true);
					objects[i].setOperational(false);
				}
    			objectMoveTimer = 10;
    		}
    		else
    			objectMoveTimer--;
    		if (objects[i].type == ObjectType.Door)
    		{
    			for (int t = 0;t<numObjects;t++)
    			{
    				if (objects[t].getX() == objects[i].getX() && objects[t].getY() == objects[i].getY())
    					objects[i].setBlocked(true);
    				else objects[i].setBlocked(false);
    			}
    		}
    		
    		if (objects[i].getX() >= (Grid.dimensionx * 10)-10 || objects[i].getY() >= (Grid.dimensiony * 10)-10 || objects[i].getX() <= 0 || objects[i].getY() <= 0 && !objects[i].isOperational())
    		{
    			grid.tiles[objects[i].getX()/10][objects[i].getY()/10].setOccupied(false);
    			deleteObject(i);
    		}
    	
	    	if (objects[i].type == ObjectType.Airlock)
	    	{
	    		main.grid.tiles[objects[i].getX()/10][objects[i].getY()/10].setHasAirlock(true);
	    	}
	    	else 
	    		main.grid.tiles[objects[i].getX()/10][objects[i].getY()/10].setHasAirlock(false);
    	}
    }
	
	
	
	 public int getNumObj()
	    {
	    	return numObjects;
	    }
	    
	    public void deleteObject (int index)
	    {
	    	if(index != -1)
	    	{
	        numObjects--;
	        for (int i = index; i < numObjects; i++)
	            objects[i]= objects[i+1];
	        objects[numObjects] = null;
	    	}
	    }
	    void deleteObject (Object obj)
	    {
	        deleteObject(getObjectIndex(obj));
	    }
	    public Object getObject(int x,int y)
	    {
	    	int index = -1;
	        for (int i = 0; i < numObjects; i++)
	        {
	        	if(objects[i].getX()==x && objects[i].getY()==y)	
	        		{
	        			index=i;
	        		}
	        }
	        if (index != -1)
	        return objects[index];
	        else
	        return null;
	    }
	    private int getObjectIndex(Object obj)
	    {
	    	int index = -1;
	    	for(int i = 0;i< numObjects;i++)
	    	{
	    		if(objects[i].equals(obj))	index=i;
	    	}
	    	return index;
	    }
	    public Object getObject(int index)
	    {
	    	return objects[index];
	    }
	    public void addObject(Object obj)
	    {
	    	objects[numObjects] = obj;
	    	numObjects++;
	    }
}
